﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Bon.It.Proxy;
using Bon_It.Model;

namespace Bonit_It.ViewModel.MasterData
{
    public class ArticleDetailsViewModel : Base.ViewModel
    {
        public ObservableCollection<Warengruppe> ComoditiyGroups { get; set; }

        public ObservableCollection<Taxes> Taxes { get; set; }

        public ObservableCollection<Gebinde> Gebinde { get; set; }

        public Artikel SelectedItem { get; set; }

        public ArticleDetailsViewModel()
        {
            
            SelectedItem = new Artikel();
            RefreshLookups();
            
            DisplayName= string.Format("Neuer Artikel");
        }


        public ArticleDetailsViewModel(Artikel item)
            : this()
        {
            SelectedItem = ServiceProxy.Instance.GetArticleById(item.ID);
            DisplayName = string.Format("Artikel: {0}", item.Bezeichnung);
        }
        #region Overrides of ViewModel

       

        public override void RefreshLookups()
        {
            ComoditiyGroups = new ObservableCollection<Warengruppe>(ServiceProxy.Instance.GetComodityGroups());
            Taxes = new ObservableCollection<Taxes>(ServiceProxy.Instance.GetTaxes());
            Gebinde = new ObservableCollection<Gebinde>(ServiceProxy.Instance.GetAllGebinde());

        }

        public override void CleanUp()
        {
            SelectedItem = null;
        }

        public override bool CanDelete(object argument)
        {
            return false;
        }

        public override bool CanSave(object argument)
        {
            return string.IsNullOrEmpty(SelectedItem.Error);
        }

        public override bool CanCreate(object argument)
        {
            return false;
        }

        public override void OnSave(object argument)
        {
            if (SelectedItem != null)
            {
                // set the modification to true;
                SelectedItem.CanBeModified = true;
                ServiceProxy.Instance.PersistArticle(SelectedItem);
            }            
            AfterSave();
        }


        #endregion
    }
}
